import { Controller, Get, Post, Body, Put, Param, Delete } from '@nestjs/common'
// import { Types } from 'mongoose'
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'
import {
  PermissionClass,
  PermissionModel,
  UpdatePermissionDto,
} from './permission.model'
import type { IResposeBase } from '../common/response'

@ApiBearerAuth()
@ApiTags('许可证管理')
@Controller('admin/acl/permission')
export class PermissionController {
  @Get('query')
  @ApiOperation({ summary: '许可证列表' })
  async findAll() {
    return await PermissionModel.find().select('-__v').exec()
  }

  @Get(':id')
  @ApiOperation({ summary: '根据自定义的id获取详情，不是mongodb的_id' })
  async findOne(@Param('id') id: number) {
    return await PermissionModel.findOne({ id }).exec()
  }

  // @Get('many/:ids')
  // @ApiOperation({ summary: '根据mongodb的_id获取集合' })
  // async findMary(@Param('ids') ids: Array<Types.ObjectId>) {
  //   const res = await PermissionModel.find({ _id: { $in: ids } }).exec()
  //   return res.filter((item) => item.code !== 'admin')
  // }

  @Post('create')
  @ApiOperation({
    summary: '创建详情',
    description: '创建一条许可内容详情',
  })
  async create(
    @Body() createPermission: PermissionClass,
  ): Promise<IResposeBase> {
    await PermissionModel.create(createPermission)
    return {
      message: '新许可内容创建成功！',
      success: true,
    }
  }

  @Put('update/:id')
  @ApiOperation({
    summary: '编辑详情',
    description: '根据id编辑一条许可内容详情',
  })
  async update(
    @Param('id') id: string,
    @Body() updatePermission: UpdatePermissionDto,
  ): Promise<IResposeBase> {
    await PermissionModel.findByIdAndUpdate(id, updatePermission, {
      runValidators: true,
      new: true, //提供应用更新后的对象
    }).exec()
    return {
      success: true,
      message: '许可内容更新成功！',
    }
  }

  @Delete('delete/:id')
  @ApiOperation({
    summary: '删除',
    description: '根据id删除一条许可内容详情',
  })
  async remove(@Param('id') id: string): Promise<IResposeBase> {
    await PermissionModel.findByIdAndDelete(id).exec()
    return {
      success: true,
      message: '许可内容删除成功！',
    }
  }
}
